package com.xul.controller;

import com.xul.entity.*;
import com.xul.service.*;
import com.xul.utils.MapController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author xul
 * @create 2021-07-07 0:16
 */

@Controller
@RequestMapping("score")
public class ScoreController {

    @Autowired
    private ScoreService scoreService;

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private SectionService sectionService;

    private final String LIST = "score/list";
    private final String ADD = "score/add";
    private final String UPDATE = "score/update";


    @GetMapping("add")
    public String add(ModelMap modelMap) {
        List<Subject> subjects = subjectService.query(null);
        modelMap.addAttribute("subjects", subjects);
        return ADD;
    }

    @PostMapping("create")
    @ResponseBody
    public Map<String, Object> create(String sectionIds, String courseIds, HttpSession session) {

        Student student = (Student) session.getAttribute("user");
        int result = scoreService.create(sectionIds, courseIds, student.getId());

        if (result <= 0) {
            return MapController.getInstance().error().getMap();
        }
        return MapController.getInstance().success().getMap();
    }

    @PostMapping("delete/{id}")
    @ResponseBody
    public Map<String, Object> delete(@PathVariable("id") Integer id) {
        int result = scoreService.delete(id);
        if (result <= 0) {
            return MapController.getInstance().error().getMap();
        }
        return MapController.getInstance().success().getMap();
    }


    @PostMapping("update")
    @ResponseBody
    public Map<String, Object> update(@RequestBody Score score) {
        int result = scoreService.update(score);
        if (result <= 0) {
            return MapController.getInstance().error().getMap();
        }
        return MapController.getInstance().success().getMap();
    }

    @GetMapping("detail/{id}")
    public String detail(@PathVariable("id") Integer id, ModelMap modelMap) {
        Score score = scoreService.detail(id);
        List<Subject> subjects = subjectService.query(null);
        modelMap.addAttribute("score", score);
        modelMap.addAttribute("subjects", subjects);
        return UPDATE;
    }


    @PostMapping("query")
    @ResponseBody
    public Map<String, Object> query(@RequestBody Score score) {
        List<Score> list = scoreService.query(score);
        List<Subject> subjects = subjectService.query(null);
        Integer count = scoreService.count(score);
        return MapController.getInstance().success().put("data", list).put("count", count).getMap();
    }

    @GetMapping("list")
    public String list() {
        return LIST;
    }

    @GetMapping("student_score")
    public String student_score() {
        return "score/student_score";
    }

    @PostMapping("query_student_score")
    @ResponseBody
    public Map<String, Object> query_student_score(HttpSession session) {
        Student student = (Student) session.getAttribute("user");
        Score score = new Score();
        score.setStuId(student.getId());
        List<Score> scores = scoreService.query(score);
        List<Course> courses = courseService.query(null);
        List<Section> sections = sectionService.query(null);
        scores.forEach(entity -> {
            courses.forEach(course -> {
                if (entity.getCourseId() == course.getId().intValue()) {
                    entity.setCourse(course);
                }
            });
            sections.forEach(section -> {
                if (entity.getSectionId() == section.getId().intValue()) {
                    entity.setSection(section);
                }
            });
            entity.setStudent(student);
        });
        return MapController.getInstance().success().put("data", scores).getMap();
    }
}
